﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ScoreMS.Model;

namespace ScoreMS.BLL
{
    /// <summary>
    /// 题目，事务逻辑
    /// </summary>
    public class ProblemBS
    {
        /// <summary>
        /// 获取单个题目
        /// </summary>
        /// <param name="course_id"></param>
        /// <returns></returns>
        static public Problem GetCourseInfo(int problem_id)
        {
            using (scoremsdbEntities db = new scoremsdbEntities())
            {
                Problem model = new Problem();
                var problem = db.problem.FirstOrDefault(x => x.problem_id == problem_id);
                model.problem_id = problem.problem_id;
                model.problem_points = problem.problem_points;
                model.problem_type = problem.problem_type;
                model.problem_difficulty = problem.problem_difficulty;
                model.ex_id = problem.ex_id;
                model.bigquestion_id = problem.bigquestion_id;
                model.smallquestion_id = problem.smallquestion_id;
                return model;
            }
        }

        /// <summary>
        /// 根据试卷ID，获取题目列表
        /// </summary>
        /// <param name="course_id"></param>
        /// <returns></returns>
        static public List<Problem> GetProblemList(int ex_id)
        {
            using (scoremsdbEntities db = new scoremsdbEntities())
            {
                //题目排列，大题号优先，小题号其次，升序
                var ret = db.problem.Where(x => x.ex_id == ex_id).OrderBy(c => c.bigquestion_id * 100000 + c.smallquestion_id);
                List<Problem> list = new List<Problem>();
                foreach (var problem in ret)
                {
                    Problem model = new Problem();
                    model.problem_id = problem.problem_id;
                    model.problem_points = problem.problem_points;
                    model.problem_type = problem.problem_type;
                    model.problem_difficulty = problem.problem_difficulty;
                    model.ex_id = problem.ex_id;
                    model.bigquestion_id = problem.bigquestion_id;
                    model.smallquestion_id = problem.smallquestion_id;
                    list.Add(model);
                }
                return list;
            }
        }

        /// <summary>
        /// 添加题目，并明确涉及的知识点（大纲）
        /// </summary>
        /// <param name="model">Model.Problem</param>
        /// <param name="syllabus_index">知识点编号, 列表</param>
        /// <returns>成功返回true</returns>
        static public bool AddProblem(Problem model, List<int> syllabus_indexList)
        {
            try
            {
                using (scoremsdbEntities db = new scoremsdbEntities())
                {
                    var problem = new problem()
                    {
                        //problem_id = model.problem_id,    自增
                        problem_difficulty = model.problem_difficulty,
                        problem_points = model.problem_points,
                        problem_type = model.problem_type,
                        bigquestion_id = model.bigquestion_id,
                        smallquestion_id = model.smallquestion_id,
                        ex_id = model.ex_id
                    };
                    if (problem.course_syllabus == null) //外键表，要自己new不？
                    {
                        problem.course_syllabus = new System.Data.Objects.DataClasses.EntityCollection<course_syllabus>();
                    }
                    string course_id = Examination_paparBS.GetCourseID(model.ex_id);

                    foreach (var syllabus_index in syllabus_indexList)
                    {
                        var target = db.course_syllabus.FirstOrDefault(c => c.course_id == course_id && c.syllabus_index == syllabus_index);
                        problem.course_syllabus.Add(target); 
                    }
                    
                    db.problem.AddObject(problem);
                    db.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
